package com.demo.cms.action.manage;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.demo.core.model.Menu;
import com.demo.core.model.Role;
import com.demo.core.model.RoleFunction;
import com.demo.core.model.Site;
import com.demo.core.service.base.MenuService;
import com.demo.core.service.base.RoleFunctionService;
import com.demo.core.service.base.RoleService;
import com.demo.core.util.collection.JsonUtils;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;

/**
 * Menu 管理Action
 * @author ryuu.kk
 */
@Function(id = 108100, pid = 10, code = "manage.menuAction")
public class MenuAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -6525752375823942510L;
	/**
	 * 菜单ID
	 */
	private Long id;
	
	/**
	 * 菜单上级ID
	 */
	private Long pid;
	
	/**
	 * 菜单列表
	 */
	private List<Menu> menuList;
	
	/**
	 * 角色列表
	 */
	private List<Role> roleList;
	
	/**
	 * 菜单模型
	 */
	private Menu menu;
	
	/**
	 * 菜单树的json字符串
	 */
	private String json;
	
	/**
	 * 菜单模型(树结构)
	 */
	private Menu tree;
	
	/**
	 * 菜单服务接口
	 */
	private MenuService menuService;
	
	/**
	 * 角色服务接口
	 */
	private RoleService roleService;
	
	/**
	 * 角色功能模型
	 */
	private RoleFunctionService roleFunctionService;
	
	/**
	 * 菜单一览
	 * @return String
	 */
	@Function(id = 108110, pid = 108100, code = "manage.menuAction.list")
	public String list() {
		// 取得站点ID
		Site site = getSite();
		this.menuList = menuService.findList(this.id, site.getId());
		return SUCCESS;
	}
	
	/**
	 * 创建菜单
	 * @return String 
	 */
	@Function(id = 108120, pid = 108100, code = "manage.menuAction.create")
	public String create() {
		
		// 取得站点ID
		Site site = getSite();
				
		permission();
		
		// 找到菜单列表
		menuList = menuService.findList(null, site.getId());
		
		return SUCCESS;
	}
	
	/**
	 * 保存菜单
	 * @return String
	 */
	@Function(id = 108130, pid = 108100, code = "manage.menuAction.save")
	public String save() {
		// 取得站点ID
		Site site = getSite();
		// 设置站点组ID
		menu.setSiteGroupId(site.getSiteGroupId());
		menu.setSiteId(site.getId());
		if (!menu.isAnonymous() && roleList != null) {
			menu.setRoleList(roleList);
		}
		
		menuService.save(menu);
		addActionMessage(getText("menu.added", "菜单已创建", menu.getTitle()));
		// function.setType(type)
		return CALLBACK;
	}
	
	@Function(id = 108140, pid = 108100, code = "manage.menuAction.edit")
	public String edit() {
		
		Site site = getSite();
		Long siteId = site.getId();
		
		permission();
		if (id != null) {
			this.menu = menuService.find(id, siteId);
		}
		return SUCCESS;
	}
	
	/**
	 * 功能更新
	 * @return String 
	 */
	@Function(id = 108150, pid = 108100, code = "manage.menuAction.update")
	public String update() {
		// 取得站点ID
		Site site = getSite();
		Long siteId = site.getId();
		// 更新功能模型
		menuService.update(id, siteId, menu);
		addActionMessage(getText("menu.updated", "菜单已更新", menu.getTitle()));
		return CALLBACK;
	}
	
	/**
	 * 删除功能信息
	 * 
	 * @return String
	 */
	@com.demo.security.annotation.function.Function(id = 108160, pid = 108100, code = "manage.function.delete")
	public String delete() {

		// 取得站点ID
		Site site = getSite();
		Long siteId = site.getId();
				
		menuService.delete(id, siteId);
		addActionMessage(getText("menu.deleted", "菜单已删除"));
		return CALLBACK;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@Function(id = 108170, pid = 108100, code = "manage.menuAction.rm")
	public String rm() {
		// 取得站点ID
		Site site = getSite();
		Long siteId = site.getId();
		
		if (menuList != null && menuList.size() > 0) {
			for (Menu m : menuList) {
				menuService.delete(m.getOriginalId(), siteId);				
			}
		}
		addActionMessage(getText("menu.deleted", "菜单已删除"));
		return CALLBACK;
	}
	
	/**
	 * 菜单自动生成
	 * @return
	 */
	public String auto() {
		
		return SUCCESS;
	}
	
	/**
	 * 查找菜单的访问许可
	 * @return
	 */
	public String permission() {
		// 取得站点ID
		Site site = getSite();
		Long siteId = site.getId();
		List<Role> roleList = roleService.findList(siteId);
		Set<Long> keySet = new HashSet<Long>();
		if (id != null && id > 0L) {
			List<RoleFunction> rfList = roleFunctionService.query(id, siteId);
			if (rfList != null && rfList.size() > 0) {
				for (RoleFunction rf : rfList){
					keySet.add(rf.getRoleId());
				}
			}
		}

		this.json = "{'items':[";
		StringBuffer sb = new StringBuffer();
		String format = "{id:\'%d\', name:\'%s\', title:\'%s\', enabled:\'%s\', isPermission:\'%s\'},";
		
		for (Role r : roleList) {
			sb.append(String.format(format, r.getId(), r.getName(), r.getTitle(), r.getEnabled(), keySet.contains(r.getId())));	
		}
		this.json += sb.toString().substring(0, sb.length() - 1) + "]}";
		return SUCCESS;
	}
	/**
	 * 菜单树
	 * @return String
	 */
	public String tree() {
		// 取得站点ID
		Site site = getSite();
		if (id != null) {
			// 站点最大功能集合(Tree),功能树下的所有
			this.tree = menuService.tree(id, site.getId());
		} else {
			// 站点最大功能集合(Tree),站点所有功能
			this.tree = menuService.tree(site.getId());
		}
		
		// 生成json树,展开级别:2
		json = JsonUtils.zTree(tree, 2);
		return SUCCESS;
	}
	
	// ~ settings here --------------------------------------------------------

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setRoleFunctionService(RoleFunctionService roleFunctionService) {
		this.roleFunctionService = roleFunctionService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public Menu getMenu() {
		return menu;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Menu getTree() {
		return tree;
	}

	public String getJson() {
		return json;
	}
	
}
